/*
 * BoardList.java
 *
 * Created on 2008/09/04 +0900
 *
 * $Revision: 38 $
 * $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */

package org.gikolet.bbs.data;

import java.util.Hashtable;
import java.util.Vector;

import org.gikolet.framework.util.Assert;

/**
 * @author tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 38 $ $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */
public class BoardList {
    private Vector categories = new Vector();
    private Hashtable boardTable = new Hashtable();

    public void addBoard(String category, Board board) {
	Assert.isNotNullAndEmpty(category);
	Assert.isNotNull(board);

	if (!this.boardTable.containsKey(category)) {
	    this.categories.addElement(category);
	    this.boardTable.put(category, new Vector());
	}

	Vector boards = (Vector) this.boardTable.get(category);

	boards.addElement(board);
    }

    public void addBoards(BoardList boardList) {
	for (int i = 0; i < boardList.getCategoryCount(); i++) {
	    String category = boardList.getCategory(i);

	    for (int j = 0; j < boardList.getBoardCount(category); j++) {
		addBoard(category, boardList.getBoard(category, j));
	    }
	}
    }

    public boolean containsCategory(String category) {
	return this.boardTable.containsKey(category);
    }

    public int indexOfCategory(String category) {
	return this.categories.indexOf(category);
    }

    public int getCategoryCount() {
	return this.categories.size();
    }

    public String getCategory(int categoryIndex) {
	Assert.isNotTrue(categoryIndex < 0);
	Assert.isNotTrue(categoryIndex >= this.categories.size());

	return (String) this.categories.elementAt(categoryIndex);
    }

    public String[] getCategories() {
	String[] categoryArray = new String[this.categories.size()];

	this.categories.copyInto(categoryArray);

	return categoryArray;
    }

    public int getBoardCount(int categoryIndex) {
	Assert.isNotTrue(categoryIndex < 0);
	Assert.isNotTrue(categoryIndex >= this.categories.size());

	String category = (String) this.categories.elementAt(categoryIndex);

	return getBoardCount(category);
    }

    public int getBoardCount(String category) {
	Assert.isNotNullAndEmpty(category);

	Vector boards = (Vector) this.boardTable.get(category);

	return boards.size();
    }

    public Board getBoard(int categoryIndex, int boardIndex) {
	Assert.isNotTrue(categoryIndex < 0);
	Assert.isNotTrue(categoryIndex >= this.categories.size());

	String category = (String) this.categories.elementAt(categoryIndex);

	return getBoard(category, boardIndex);
    }

    public Board getBoard(String category, int boardIndex) {
	Assert.isNotNullAndEmpty(category);

	Vector boards = (Vector) this.boardTable.get(category);

	Assert.isNotTrue(boardIndex < 0);
	Assert.isNotTrue(boardIndex >= boards.size());

	return (Board) boards.elementAt(boardIndex);
    }

    public Board[] getBoards(String category) {
	Assert.isNotNullAndEmpty(category);

	Vector boards = (Vector) this.boardTable.get(category);

	Board[] boardArray = new Board[boards.size()];
	boards.copyInto(boardArray);

	return boardArray;
    }
}
